//
//  TLTweetViewController.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/22.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLTweetViewController.h"
#import "TLTweetSquareViewController.h"
#import "TLMomentsViewController.h"
#import "TLHotTweetViewController.h"
#import "TLSendTweetViewController.h"
#import "TLNavigationViewController.h"
#import "TLHttpTool.h"
#import "TLNavigationBar.h"

@interface TLTweetViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *childVcScrollView;

@property (nonatomic, weak) UIView *titleView;

@end

@implementation TLTweetViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addAllChildVcs];
    
    [self addContentScrollView];
    
    [self setupTitleView];
   
    [self addSubViews];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNavBarFrame:) name:@"scroll" object:nil];
}

- (void)changeNavBarFrame:(NSNotification *)notification
{
    NSDictionary *dict = notification.userInfo;
    BOOL scrollUp = [[dict objectForKey:@"direction"] boolValue];
    CGFloat offsetDistance = [[dict objectForKey:@"offsetDistance"] floatValue];
    [UIView animateWithDuration:0.5 animations:^{
        
        if(scrollUp){ // 向上滑动
            if(offsetDistance >= 64.0){
                self.navBar.frame = CGRectMake(0, -64, TLScreenW, 64);
                self.titleView.frame = CGRectMake(0, 0, TLScreenW, 64);
            }
        }else{  // 向下滑动
            self.navBar.frame = CGRectMake(0, 0, TLScreenW, 64);
            self.titleView.frame = CGRectMake(0, 64, TLScreenW, 44);
        }
    }];
}

/**
 *  添加所有的子控制器
 */
- (void)addAllChildVcs
{
    TLTweetSquareViewController *tweetSquareVc = [[TLTweetSquareViewController alloc] init];
    [self addChildViewController:tweetSquareVc];
    
    TLMomentsViewController *momentsVc = [[TLMomentsViewController alloc] init];
    [self addChildViewController:momentsVc];
    
    TLHotTweetViewController *hotTweetVc = [[TLHotTweetViewController alloc] init];
    [self addChildViewController:hotTweetVc];
}

/**
 *  添加标题栏
 */
- (void)setupTitleView
{
    // 标签栏整体
    UIView *titleView = [[UIView alloc] init];
    titleView.frame = CGRectMake(0, 64, self.view.width, 44);
    titleView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
//    // 标签栏按钮
//    NSUInteger count = self.childViewControllers.count;
//    CGFloat titleButtonH = titleView.height;
//    CGFloat titleButtonW = titleView.width / count;
//    for (int i = 0; i < count; i++) {
//        // 创建
//        LCTitleButton *titleButton = [LCTitleButton buttonWithType:UIButtonTypeCustom];
//        [titleButton addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [titleView addSubview:titleButton];
//        [self.titleButtons addObject:titleButton];
//
//        // 文字
//        NSString *title = [self.childViewControllers[i] title];
//        [titleButton setTitle:title forState:UIControlStateNormal];
//
//        // frame
//        titleButton.y = 0;
//        titleButton.height = titleButtonH;
//        titleButton.width = titleButtonW;
//        titleButton.x = i * titleButton.width;
//    }
//    // 标签栏底部的指示器控件
//    UIView *indicateView = [[UIView alloc] init];
//    indicateView.backgroundColor = [self.titleButtons.lastObject titleColorForState:UIControlStateSelected];
//    indicateView.height = 1;
//    indicateView.y = titleView.height - indicateView.height;
//    [titleView addSubview:indicateView];
//    self.indicateView = indicateView;
//
//    // 默认第一个按钮被点击
//    LCTitleButton *firstTitleBtn = self.titleButtons.firstObject;
//    [self titleBtnClick:firstTitleBtn];
}

- (void)addContentScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *childVcScrollView = [[UIScrollView alloc] init];
    childVcScrollView.frame = self.view.frame;
    childVcScrollView.pagingEnabled = YES;
    childVcScrollView.delegate = self;
    childVcScrollView.showsVerticalScrollIndicator = NO;
    childVcScrollView.showsHorizontalScrollIndicator = NO;
    childVcScrollView.contentSize = CGSizeMake(self.childViewControllers.count * TLScreenW, 0);
    [self.view addSubview:childVcScrollView];
    self.childVcScrollView = childVcScrollView;
    
    [self scrollViewDidEndScrollingAnimation:self.childVcScrollView];
}

- (void)addSubViews
{
    self.title = @"冒泡";
    
    self.customerNavigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"hot_topic_Nav" highImageName:@"hot_topic_Nav" target:self action:@selector(jumpToTopic)];
    
    self.customerNavigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"tweetBtn_Nav" highImageName:@"tweetBtn_Nav" target:self action:@selector(sendTweets)];
}

- (void)jumpToTopic
{
    TLLog(@"%s", __func__);
}

- (void)sendTweets
{
    TLSendTweetViewController *sendTweetVc = [[TLSendTweetViewController alloc] init];
    TLNavigationViewController *sendTweetNav = [[TLNavigationViewController alloc] initWithRootViewController:sendTweetVc];
    [self presentViewController:sendTweetNav animated:YES completion:nil];
    TLLog(@"%s", __func__);
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    UIViewController *childVc = self.childViewControllers[index];
    if(childVc.isViewLoaded){
        return;
    }
    
    childVc.view.frame = scrollView.bounds;
    [scrollView addSubview:childVc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}



@end
